# -*- coding: utf-8 -*-
# 1、这里只是以ocr下的RecognizeBankCard能力为例，其他能力请引入相应类目的包和相关类
from alibabacloud_facebody20191230.client import Client
from alibabacloud_facebody20191230.models import GenerateHumanAnimeStyleRequest, GenerateHumanAnimeStyleAdvanceRequest
from alibabacloud_oss_util.models import RuntimeOptions
from alibabacloud_tea_openapi.models import Config
from urllib.request import urlopen
import io
import os
import ssl


def human_anime(image_url, algo_type):
    '''银行卡示例'''
    # 初始化Config
    config = Config(
        # 2、"YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET" 的生成请参考https://help.aliyun.com/document_detail/175144.html
        # 如果您是用的子账号AccessKey，还需要为子账号授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html
        # 您的 AccessKey ID
        access_key_id=os.getenv('ALIYUN_ACCESS_KEY'),
        # 您的 AccessKey Secret
        access_key_secret=os.getenv('ALIYUN_ACCESS_SECRET'),
        # 3、访问的域名。注意：这个地方需要求改为相应类目的域名，参考：https://help.aliyun.com/document_detail/143103.html
        endpoint='facebody.cn-shanghai.aliyuncs.com',
        # 访问的域名对应的region
        region_id='cn-shanghai'
    )

    # 4、初始化Request，这里只是以RecognizeBankCard为例，其他能力请使用相应能力对应的类
    # request = GenerateHumanAnimeStyleRequest(image_url=image_url, algo_type=algo_type)

    # 初始化RuntimeObject
    runtime_option = RuntimeOptions()

    try:
        # 初始化Client
        client = Client(config)
        context = ssl._create_unverified_context()
        img = urlopen(image_url, context=context).read()
        request = GenerateHumanAnimeStyleAdvanceRequest(algo_type=algo_type)
        request.image_urlobject = io.BytesIO(img)

        # 5、调用api，注意，recognize_bank_card_with_options需要更换为相应能力对应的方法名
        response = client.generate_human_anime_style_advance(request, runtime_option)

        # 获取整体结果
        print(response.body)
        return response.body
    except Exception as error:
        # 获取整体报错信息
        print(error)
        # 获取单个字段
        print(error.code)
        # tips: 可通过error.__dict__查看属性名称
        return error
